﻿using System;
using System.IO;
using System.Collections.Generic;

namespace TestMiniAccess
{
    using Ruandao.MiniAccess.SqlServer;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Ruandao.MiniAccess;
    using System.Configuration;


    [TestClass()]
    public class DbUtilityTest
    {
        static string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
        private static Database testDatabase = new SqlDatabase(connString);

        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性

        [TestInitialize()]
        public void MyTestInitialize()
        {
            string file = Path.Combine(TestContext.TestDeploymentDir, "ResetData.xml");
            new SqlCommandFile(file).ExecuteCommand();
        }
        
        #endregion

        [TestMethod()]
        public void Manual_Test()
        {
            string connstr = "Data Source=.\\;Initial Catalog=master;Integrated Security=True";
            System.Nullable<System.DateTime> actual;
            string path = new DirectoryInfo(this.TestContext.TestDir + "../../../TestMisc").FullName;

            actual = DbUtility_Accessor.DbCreateFileDateTime("tempdb", connstr);
            Console.WriteLine("数据库tempdb对应的数据库文件的创建日期时间是否是{0}", actual);

            actual = DbUtility_Accessor.DbCreateDate("tempdb", connstr);
            Console.WriteLine("数据库tempdb的创建日期（注意Sql2000中没有时间只有日期）是否是{0}", actual);

            Assert.Inconclusive("请手工检查测试的输出是否正确。注意，文件和文件夹的日期检测在win98下不支持。");
        }

        [TestMethod()]
        public void GetRecordNumberTest()
        {
            int expected = 5;
            int actual;
            actual = DbUtility.GetRecordNumber(testDatabase, "Test");
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckRecordTest()
        {
            // Id,FirstName,LastName,Sex,Age,HeadImage,Description
            // 1,'first1',   'last1',  'sex1',10,0x11223344,'ntext...'
            Dictionary<string, object> keys = new Dictionary<string, object>();
            keys.Add("Id", 1);

            Dictionary<string, object> values = new Dictionary<string, object>();
            values.Add("FirstName", "first1");
            values.Add("LastName", "last1");
            values.Add("Sex", "sex1");
            values.Add("Age", 10);
            byte[] bytes = new byte[4];
            bytes[0] = 0x11;
            bytes[1] = 0x22;
            bytes[2] = 0x33;
            bytes[3] = 0x44;
            values.Add("HeadImage", bytes );
            values.Add("Description", "ntext...");

            string expected = "";
            Assert.AreEqual(expected, DbUtility.CheckRecord(testDatabase, "Test", keys, values));
        }
    }
}
